package com.wl156.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ksoap2.serialization.SoapObject;

import com.wl156.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckableAdapter extends BaseAdapter {
	private HashMap<Integer, Boolean> mCheckedMap = new HashMap<Integer, Boolean>();

	public static final int DEFAULT = 0;
	public static final int COLLECT = 1;

	protected static final String TAG = null;
	protected LayoutInflater mInflater;
	protected SoapObject mData;
	protected Context mContext;
	protected int mType;

	public List<Integer> getCheckedList() {
		List<Integer> checks = new ArrayList<Integer>();
		for (Map.Entry<Integer, Boolean> entry : mCheckedMap.entrySet()) {
			if (entry.getValue()) {
				checks.add(entry.getKey());
			}
		}
		return checks;
	}

	@Override
	public int getCount() {
		if (mData == null) {
			return 0;
		}
		int c = mData.getPropertyCount();
		Log.d(TAG, "c: " + c);
		return c;
	}

	@Override
	public SoapObject getItem(int position) {
		return (SoapObject) mData.getProperty(position);
	}

	@Override
	public long getItemId(int position) {
		try {
			return Long.valueOf(getItem(position).getProperty(0).toString());
		} catch (Exception e) {
			return 0;
		}
	}

	class MyOnCheckedChangeListener implements OnCheckedChangeListener {
		int position;

		public MyOnCheckedChangeListener(int position) {
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			mCheckedMap.put(position, isChecked);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			final CheckBox checked = (CheckBox) convertView
					.findViewById(R.id.checked);
			checked.setOnCheckedChangeListener(new MyOnCheckedChangeListener(
					position));
		} catch (Exception e) {
		}
		return null;
	}

	protected String getTextViewStringFromProperty(int position, int index) {
		try {
			Object prop = getItem(position).getProperty(index);
			String text = "";
			if (prop != null) {
				text = prop.toString();
			}
			if (text.equals("anyType{}")) {
				text = "";
			}
			return text;
		} catch (Exception e) {
			return "";
		}
	}

	protected void setTextViewStringFromProperty(int position, TextView tv,
			int index) {
		try {
			Object prop = getItem(position).getProperty(index);
			String text = "";
			if (prop != null) {
				text = prop.toString();
			}
			if (text.equals("anyType{}")) {
				text = "";
			}
			tv.setText(text);
		} catch (Exception e) {
		}
	}

	protected void setPhoneView(int position, TextView view, int index) {
		try {
			Object prop = getItem(position).getProperty(index);
			if (prop == null) {
				return;
			}
			String phone = prop.toString();
			if (phone.equals("anyType{}")) {
				phone = "";
				view.setVisibility(View.INVISIBLE);
			} else {
				view.setTag(phone);
			}
		} catch (Exception e) {
		}
	}
}
